<?php
/**
 * 产生一个指定长度的随机字符串,并返回给用户
 * @param type $len 产生字符串的长度
 * @return string 随机字符串
 */
if ( ! function_exists('get_random_string'))
{
	function get_random_string($len = 6)
	{
		$chars = array(
	        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
	        "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
	        "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
	        "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
	        "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
	        "3", "4", "5", "6", "7", "8", "9"
	        );
	        $charsLen = count($chars) - 1;
	        // 将数组打乱
	        shuffle($chars);
	        $output = "";
	        for ($i = 0; $i < $len; $i++) {
	        	$output .= $chars[mt_rand(0, $charsLen)];
	        }
	        return $output;
	}
}


/**
 * 获取客户端IP地址
 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
 * @param boolean $adv 是否进行高级模式获取（有可能被伪装）
 * @return mixed
 */
if ( ! function_exists('get_client_ip'))
{
	function get_client_ip($type = 0, $adv = false) {
		$type = $type ? 1 : 0;
		static $ip = NULL;
		if ($ip !== NULL)
		return $ip[$type];
		if ($adv) {
			if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
				$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
				$pos = array_search('unknown', $arr);
				if (false !== $pos)
				unset($arr[$pos]);
				$ip = trim($arr[0]);
			}elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
				$ip = $_SERVER['HTTP_CLIENT_IP'];
			} elseif (isset($_SERVER['REMOTE_ADDR'])) {
				$ip = $_SERVER['REMOTE_ADDR'];
			}
		} elseif (isset($_SERVER['REMOTE_ADDR'])) {
			$ip = $_SERVER['REMOTE_ADDR'];
		}
		// IP地址合法验证
		$long = sprintf("%u", ip2long($ip));
		$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
		return $ip[$type];
	}
}

/**
 * XML编码
 * @param mixed $data 数据
 * @param string $root 根节点名
 * @param string $item 数字索引的子节点名
 * @param string $attr 根节点属性
 * @param string $id   数字索引子节点key转换的属性名
 * @param string $encoding 数据编码
 * @return string
 */
if ( ! function_exists('xml_encode'))
{
	function xml_encode($data, $root='think', $item='item', $attr='', $id='id', $encoding='utf-8') {
		if(is_array($attr)){
			$_attr = array();
			foreach ($attr as $key => $value) {
				$_attr[] = "{$key}=\"{$value}\"";
			}
			$attr = implode(' ', $_attr);
		}
		$attr   = trim($attr);
		$attr   = empty($attr) ? '' : " {$attr}";
		$xml    = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>";
		$xml   .= "<{$root}{$attr}>";
		$xml   .= data_to_xml($data, $item, $id);
		$xml   .= "</{$root}>";
		return $xml;
	}
}

/**
 * 复制一个目录下面的所有文件到另一个目录
 * @param string $src 源目录
 * @param string $dst 目标目录
 */
if( ! function_exists('recurse_copy')){
	function recurse_copy($src, $dst){
		$dir = opendir($src);
		@mkdir($dst);
		while(false!==($file=readdir($dir))){
			if(($file!='.')&&($file!='..')){
				if(is_dir($src.'/'.$file)){
					recurse_copy($src.'/'.$file, $dst.'/'.$file);
				}else{
					copy($src.'/'.$file,$dst.'/'.$file);
				}
			}
		}
		closedir($dir);
	}
}

/**
 * 删除文件目录以及其子目录及文件
 * @param string $dir 文件目录
 */
if( ! function_exists('del_dir')){
	function del_dir($dir){
		//先删除目录下的文件
		$dh = opendir($dir);
		while($file = readdir($dh)){
			if($file!='.'&&$file!='..'){
				$fulepath = $dir.'/'.$file;
				if(!is_dir($fulepath)){
					unlink($fulepath);
				}else{
					del_dir($fulepath);
				}
			}
		}
		closedir($dh);
		//删除当前文件夹
		if(rmdir($dir)){
			return true;
		}else{
			return false;
		}
	}
}

/**
 * @param $dir
 * @param int $mode
 * @return bool
 */
function dmkdir($dir, $mode = 0777)
{
	if(!is_dir($dir))
	{
		dmkdir(dirname($dir), $mode);
		@mkdir($dir, $mode);
	}
	return true;
}
/**
 * 获取价格格式化信息
 * @param $price
 * @return string
 */
if ( ! function_exists('price_format'))
{
	function price_format($price){
		return sprintf('￥%1.2f元',$price);
	}
}
/**
 * 截取字符串
 * @param $str 字符串
 * @param $start 开始位置
 * @param $end   结束位置
 * @param string $charset 字符编码
 * @return mixed
 */
if ( ! function_exists('sub_str'))
{
	function sub_str($str,$start,$end,$charset='utf-8'){
		return mb_substr($str,$start,$end,$charset);
	}
}

if(! function_exists('get_thumb')){
	function get_thumb($orginal_img)
	{
		return filter_img($orginal_img);
	}
}

if(! function_exists('get_diy')){
	function diy($position, $type, $template = '',$echo = 1)
	{
		static $CI = null;
		if($CI == null){
			$CI = new CI_Module();
		}
		$CI->load->model('diy.diy_model');
		if($echo){
			echo $CI->diy_model->get_diy($position, $type, $template);
		}else{
			return $CI->diy_model->get_diy($position, $type, $template);
		}
	}
}

if(!function_exists('cutstr'))
{
	function cutstr($string, $length, $dot = '...') {
		if(strlen($string) <= $length) {
			return $string;
		}

		$pre = chr(1);
		$end = chr(1);
		$string = str_replace(array('&amp;', '&quot;', '&lt;', '&gt;'), array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), $string);

		$strcut = '';
		if('utf-8') {

			$n = $tn = $noc = 0;
			while($n < strlen($string)) {

				$t = ord($string[$n]);
				if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
					$tn = 1; $n++; $noc++;
				} elseif(194 <= $t && $t <= 223) {
					$tn = 2; $n += 2; $noc += 2;
				} elseif(224 <= $t && $t <= 239) {
					$tn = 3; $n += 3; $noc += 2;
				} elseif(240 <= $t && $t <= 247) {
					$tn = 4; $n += 4; $noc += 2;
				} elseif(248 <= $t && $t <= 251) {
					$tn = 5; $n += 5; $noc += 2;
				} elseif($t == 252 || $t == 253) {
					$tn = 6; $n += 6; $noc += 2;
				} else {
					$n++;
				}

				if($noc >= $length) {
					break;
				}

			}
			if($noc > $length) {
				$n -= $tn;
			}

			$strcut = substr($string, 0, $n);

		} else {
			$_length = $length - 1;
			for($i = 0; $i < $length; $i++) {
				if(ord($string[$i]) <= 127) {
					$strcut .= $string[$i];
				} else if($i < $_length) {
					$strcut .= $string[$i].$string[++$i];
				}
			}
		}

		$strcut = str_replace(array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), array('&amp;', '&quot;', '&lt;', '&gt;'), $strcut);

		$pos = strrpos($strcut, chr(1));
		if($pos !== false) {
			$strcut = substr($strcut,0,$pos);
		}
		return $strcut.$dot;
	}
}

if(!function_exists('parse_img'))
{
	function parse_img($string, $allow_remote = 0)
	{
		$pattern = '/<img.*\s+src=([\'"]?)([^\'"]+(jpg|gif|bmp|bnp|png))\\1.+>/iU';
		preg_match_all($pattern, $string,$m);
		$image_list = $m[2];
		foreach ($image_list as $k => $v)
		{
			if(!$allow_remote){
				if(strpos($v, 'http://') !== FALSE || strpos($v, 'HTTP://') !== FALSE)
				{
					unset($image_list[$k]);
				}
			}
		}
		$image_list = array_values($image_list);
		return $image_list;
	}
}

/**
 * 汉字转化为拼音
 * @param $_String
 * @return string
 */
function Pinyin($_String, $_Code='utf8')
{
	$_DataKey = "a|ai|an|ang|ao|ba|bai|ban|bang|bao|bei|ben|beng|bi|bian|biao|bie|bin|bing|bo|bu|ca|cai|can|cang|cao|ce|ceng|cha".
"|chai|chan|chang|chao|che|chen|cheng|chi|chong|chou|chu|chuai|chuan|chuang|chui|chun|chuo|ci|cong|cou|cu|".
"cuan|cui|cun|cuo|da|dai|dan|dang|dao|de|deng|di|dian|diao|die|ding|diu|dong|dou|du|duan|dui|dun|duo|e|en|er".
"|fa|fan|fang|fei|fen|feng|fo|fou|fu|ga|gai|gan|gang|gao|ge|gei|gen|geng|gong|gou|gu|gua|guai|guan|guang|gui".
"|gun|guo|ha|hai|han|hang|hao|he|hei|hen|heng|hong|hou|hu|hua|huai|huan|huang|hui|hun|huo|ji|jia|jian|jiang".
"|jiao|jie|jin|jing|jiong|jiu|ju|juan|jue|jun|ka|kai|kan|kang|kao|ke|ken|keng|kong|kou|ku|kua|kuai|kuan|kuang".
"|kui|kun|kuo|la|lai|lan|lang|lao|le|lei|leng|li|lia|lian|liang|liao|lie|lin|ling|liu|long|lou|lu|lv|luan|lue".
"|lun|luo|ma|mai|man|mang|mao|me|mei|men|meng|mi|mian|miao|mie|min|ming|miu|mo|mou|mu|na|nai|nan|nang|nao|ne".
"|nei|nen|neng|ni|nian|niang|niao|nie|nin|ning|niu|nong|nu|nv|nuan|nue|nuo|o|ou|pa|pai|pan|pang|pao|pei|pen".
"|peng|pi|pian|piao|pie|pin|ping|po|pu|qi|qia|qian|qiang|qiao|qie|qin|qing|qiong|qiu|qu|quan|que|qun|ran|rang".
"|rao|re|ren|reng|ri|rong|rou|ru|ruan|rui|run|ruo|sa|sai|san|sang|sao|se|sen|seng|sha|shai|shan|shang|shao|".
"she|shen|sheng|shi|shou|shu|shua|shuai|shuan|shuang|shui|shun|shuo|si|song|sou|su|suan|sui|sun|suo|ta|tai|".
"tan|tang|tao|te|teng|ti|tian|tiao|tie|ting|tong|tou|tu|tuan|tui|tun|tuo|wa|wai|wan|wang|wei|wen|weng|wo|wu".
"|xi|xia|xian|xiang|xiao|xie|xin|xing|xiong|xiu|xu|xuan|xue|xun|ya|yan|yang|yao|ye|yi|yin|ying|yo|yong|you".
"|yu|yuan|yue|yun|za|zai|zan|zang|zao|ze|zei|zen|zeng|zha|zhai|zhan|zhang|zhao|zhe|zhen|zheng|zhi|zhong|".
"zhou|zhu|zhua|zhuai|zhuan|zhuang|zhui|zhun|zhuo|zi|zong|zou|zu|zuan|zui|zun|zuo";
	$_DataValue = "-20319|-20317|-20304|-20295|-20292|-20283|-20265|-20257|-20242|-20230|-20051|-20036|-20032|-20026|-20002|-19990".
"|-19986|-19982|-19976|-19805|-19784|-19775|-19774|-19763|-19756|-19751|-19746|-19741|-19739|-19728|-19725".
"|-19715|-19540|-19531|-19525|-19515|-19500|-19484|-19479|-19467|-19289|-19288|-19281|-19275|-19270|-19263".
"|-19261|-19249|-19243|-19242|-19238|-19235|-19227|-19224|-19218|-19212|-19038|-19023|-19018|-19006|-19003".
"|-18996|-18977|-18961|-18952|-18783|-18774|-18773|-18763|-18756|-18741|-18735|-18731|-18722|-18710|-18697".
"|-18696|-18526|-18518|-18501|-18490|-18478|-18463|-18448|-18447|-18446|-18239|-18237|-18231|-18220|-18211".
"|-18201|-18184|-18183|-18181|-18012|-17997|-17988|-17970|-17964|-17961|-17950|-17947|-17931|-17928|-17922".
"|-17759|-17752|-17733|-17730|-17721|-17703|-17701|-17697|-17692|-17683|-17676|-17496|-17487|-17482|-17468".
"|-17454|-17433|-17427|-17417|-17202|-17185|-16983|-16970|-16942|-16915|-16733|-16708|-16706|-16689|-16664".
"|-16657|-16647|-16474|-16470|-16465|-16459|-16452|-16448|-16433|-16429|-16427|-16423|-16419|-16412|-16407".
"|-16403|-16401|-16393|-16220|-16216|-16212|-16205|-16202|-16187|-16180|-16171|-16169|-16158|-16155|-15959".
"|-15958|-15944|-15933|-15920|-15915|-15903|-15889|-15878|-15707|-15701|-15681|-15667|-15661|-15659|-15652".
"|-15640|-15631|-15625|-15454|-15448|-15436|-15435|-15419|-15416|-15408|-15394|-15385|-15377|-15375|-15369".
"|-15363|-15362|-15183|-15180|-15165|-15158|-15153|-15150|-15149|-15144|-15143|-15141|-15140|-15139|-15128".
"|-15121|-15119|-15117|-15110|-15109|-14941|-14937|-14933|-14930|-14929|-14928|-14926|-14922|-14921|-14914".
"|-14908|-14902|-14894|-14889|-14882|-14873|-14871|-14857|-14678|-14674|-14670|-14668|-14663|-14654|-14645".
"|-14630|-14594|-14429|-14407|-14399|-14384|-14379|-14368|-14355|-14353|-14345|-14170|-14159|-14151|-14149".
"|-14145|-14140|-14137|-14135|-14125|-14123|-14122|-14112|-14109|-14099|-14097|-14094|-14092|-14090|-14087".
"|-14083|-13917|-13914|-13910|-13907|-13906|-13905|-13896|-13894|-13878|-13870|-13859|-13847|-13831|-13658".
"|-13611|-13601|-13406|-13404|-13400|-13398|-13395|-13391|-13387|-13383|-13367|-13359|-13356|-13343|-13340".
"|-13329|-13326|-13318|-13147|-13138|-13120|-13107|-13096|-13095|-13091|-13076|-13068|-13063|-13060|-12888".
"|-12875|-12871|-12860|-12858|-12852|-12849|-12838|-12831|-12829|-12812|-12802|-12607|-12597|-12594|-12585".
"|-12556|-12359|-12346|-12320|-12300|-12120|-12099|-12089|-12074|-12067|-12058|-12039|-11867|-11861|-11847".
"|-11831|-11798|-11781|-11604|-11589|-11536|-11358|-11340|-11339|-11324|-11303|-11097|-11077|-11067|-11055".
"|-11052|-11045|-11041|-11038|-11024|-11020|-11019|-11018|-11014|-10838|-10832|-10815|-10800|-10790|-10780".
"|-10764|-10587|-10544|-10533|-10519|-10331|-10329|-10328|-10322|-10315|-10309|-10307|-10296|-10281|-10274".
"|-10270|-10262|-10260|-10256|-10254";
	$_TDataKey = explode('|', $_DataKey);
	$_TDataValue = explode('|', $_DataValue);
	$_Data = (PHP_VERSION>='5.0') ? array_combine($_TDataKey, $_TDataValue) : _Array_Combine($_TDataKey, $_TDataValue);
	arsort($_Data);
	reset($_Data);
	if($_Code != 'gb2312') $_String = _U2_Utf8_Gb($_String);
	$_Res = '';
	for($i=0; $i<strlen($_String); $i++)
	{
		$_P = ord(substr($_String, $i, 1));
		if($_P>160) { $_Q = ord(substr($_String, ++$i, 1)); $_P = $_P*256 + $_Q - 65536; }
		$_Res .= _Pinyin($_P, $_Data);
	}
	return preg_replace("/[^a-z0-9]*/", '', $_Res);
}
function _Pinyin($_Num, $_Data)
{
	if ($_Num>0 && $_Num<160 ) return chr($_Num);
	elseif($_Num<-20319 || $_Num>-10247) return '';
	else {
		foreach($_Data as $k=>$v){ if($v<=$_Num) break; }
		return $k;
	}
}
function _U2_Utf8_Gb($_C)
{
	$_String = '';
	if($_C < 0x80) $_String .= $_C;
	elseif($_C < 0x800)
	{
		$_String .= chr(0xC0 | $_C>>6);
		$_String .= chr(0x80 | $_C & 0x3F);
	}elseif($_C < 0x10000){
		$_String .= chr(0xE0 | $_C>>12);
		$_String .= chr(0x80 | $_C>>6 & 0x3F);
		$_String .= chr(0x80 | $_C & 0x3F);
	} elseif($_C < 0x200000) {
		$_String .= chr(0xF0 | $_C>>18);
		$_String .= chr(0x80 | $_C>>12 & 0x3F);
		$_String .= chr(0x80 | $_C>>6 & 0x3F);
		$_String .= chr(0x80 | $_C & 0x3F);
	}
	return iconv('UTF-8', 'GB2312', $_String);
}
function _Array_Combine($_Arr1, $_Arr2)
{
	for($i=0; $i<count($_Arr1); $i++) $_Res[$_Arr1[$i]] = $_Arr2[$i];
	return $_Res;
}
if(!function_exists('http_post_data')){
	function http_post_data($url, $data_string) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($data_string))
		);
		ob_start();
		curl_exec($ch);
		$return_content = ob_get_contents();
		ob_end_clean();

		return @json_decode($return_content, TRUE);
	}
}

function http_post_formdata($url, $field, $file){

	$fields[$field] = '@'.$file;

	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, $url );
	curl_setopt($ch, CURLOPT_POST, 1 );
	curl_setopt($ch, CURLOPT_POSTFIELDS, $fields );

	ob_start();
	curl_exec( $ch );
	$return_content = ob_get_contents();
	ob_end_clean();

	curl_close($ch);
	return @json_decode($return_content, TRUE);
}

/**
 * 对变量进行 JSON 编码
 * @param mixed value 待编码的 value ，除了resource 类型之外，可以为任何数据类型，该函数只能接受 UTF-8 编码的数据
 * @return string 返回 value 值的 JSON 形式
 */
function json_encode_ex($value)
{
	if (version_compare(PHP_VERSION,'5.4.0','<'))
	{
		$str = json_encode($value);
		$str = preg_replace_callback(
            "#\\\u([0-9a-f]{4})#i",
            'iconv_matches',
		$str
		);
		return $str;
	}
	else
	{
		return json_encode($value, JSON_UNESCAPED_UNICODE);
	}
}
function iconv_matches($matchs){
	return iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1]));
}

/**
 * 图片路径过滤修改
 */
if(!function_exists('filter_img'))
{
	function filter_img($str){
		$str = str_replace(array('/uploadfile/'),array('/'),$str);
		if(strpos($str,'http')!==FALSE){
			return $str;
		}
		return URLPIC.$str;
	}
}

function filter_content($str){
	$str = str_replace(array('/uploadfile/','/youhao/'),array('/','/youhaotravel/'),$str);
//    return preg_replace('/(<img.*\s+src=[\'"])([^http])(.+\.(jpg|gif|bmp|bnp|png)\"?.+\/>)/i',"\${1}".URLPIC."\${2}\${3}",$str);
    return preg_replace('/(<[img|IMG].*?src=[\'|\"])([^http])(.*?(?:[\.jpg|\.gif|\.bmp|\.bnp|\.png]))([\'|\"].*?[\/]?>)/i',"$1".URLPIC."$3$4",$str);
}


/**
 * 判断手机端
 */
if(!function_exists('check_wap'))
{
	function check_wap()
	{
		if(stristr(@$_SERVER['HTTP_VIA'],"wap")){// 先检查是否为wap代理，准确度高
			return true;
		}elseif(strpos(strtoupper(@$_SERVER['HTTP_ACCEPT']),"VND.WAP.WML") > 0){// 检查浏览器是否接受 WML.
			return true;
		}elseif(preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i', $_SERVER['HTTP_USER_AGENT'])){//检查USER_AGENT
			return true;
		}else{
			return false;
		}
	}
}

/**
 * 广告位广告信息输出
 * @param $id 广告位置ID
 * @param int $limit 限制数量，如果为一时，则输出完整的广告html
 * @return mixed
 */
if(!function_exists('output_advs'))
{
    function output_advs($id,$limit=1){
        $CI = new CI_Module();
        $CI->load->model('advert.Adresource_model');
        $list = $CI->Adresource_model->get_ads($id,$limit);
        if($limit==1){
            return @$list[0];
        }else{
            return $list;
        }
    }
}

/**
 * 多个广告位广告信息输出
 * @param $data array('8'=>1,'14'=>1,'9'=>1,'10'=>2,'11'=>1,'12'=>1,'13'=>2,'15'=>1) ad_id num
 * @return mixed
 */
if(!function_exists('output_advs_list')) {
    function output_advs_list($data)
    {
        $CI = new CI_Module();
        $CI->load->model('advert.Adresource_model');
        $list = $CI->Adresource_model->get_ads_list($data);
        return $list;
    }
}

/**
 * 将数字转换为汉字，比如1210转换为一千二百一十
 */
if(!function_exists('num2ch'))
{
	function num2ch($num) //整体读取转换
	{
		$num_real = del0($num);//去掉前面的“0”
		$numlen = strlen($num_real);
		if($numlen >= 9)//如果满九位，读取“亿”位
		{
			$y=substr($num_real, -9, 1);
			$wsbq = substr($num_real, -8, 4);
			$gsbq = substr($num_real, -4);
			$a = num_r(del0($gsbq));
			$b = num_r(del0($wsbq))."万";
			$c = num_r(del0($y))."亿";
		}elseif($numlen <= 8 && $numlen >= 5) //如果大于等于“万”
		{
			$wsbq = substr($num_real, 0, $numlen-4);
			$gsbq = substr($num_real, -4);
			$a = num_r(del0($gsbq));
			$b = num_r(del0($wsbq))."万";
			$c="";
		}elseif($numlen <= 4) //如果小于等于“千”
		{
			$gsbq = substr( $num_real, -$numlen);
			$a = num_r(del0($gsbq));
			$b="";
			$c="";
		}
		$ch_num = $c.$b.$a;
		return $ch_num;
	}
}

/**
 * 去掉数字段前面的0
 */
if(!function_exists('del0'))
{
	function del0($num)
	{
		return "".intval($num);
	}
}

/**
 * 单个数字变汉字
 */
if(!function_exists('n2c'))
{
	function n2c($x)
	{
		$arr_n = array("零","一","二","三","四","五","六","七","八","九","十");
		return $arr_n[$x];
	}
}

/**
 * 时间戳转化
 */
if(!function_exists('transform_time')) {

    function transform_time($time)
    {
        $rtime = date("m-d H:i", $time);
        $htime = date("H:i", $time);
        $time = time() - $time;
        if ($time < 60) {
            $str = '刚刚';
        } elseif ($time < 60 * 60) {
            $min = floor($time / 60);
            $str = $min . '分钟前';
        } elseif ($time < 60 * 60 * 24) {
            $h = floor($time / (60 * 60));
            $str = $h . '小时前 ' . $htime;
        } elseif ($time < 60 * 60 * 24 * 3) {
            $d = floor($time / (60 * 60 * 24));
            if ($d == 1)
                $str = '昨天 ' . $rtime;
            else
                $str = '前天 ' . $rtime;
        }
        else {
            $str = $rtime;
        }
        return $str;
    }
}

/**
 * 读取数值（4位）
 */
if(!function_exists('num_r'))
{
	function num_r($abcd)
	{
		$arr= array();
		$str = ""; //读取后的汉字数值
		$flag = 0; //该位是否为零
		$flag_end = 1; //是否以“零”结尾
		$size_r = strlen($abcd);
		for($i=0; $i<$size_r; $i++)
		{
			$arr[$i] = $abcd{$i};
		}
		$arrlen = count($arr);
		for($j=0; $j<$arrlen; $j++)
		{
			$ch = n2c($arr[$arrlen-1-$j]); //从后向前转汉字
			if($ch == "零" && $flag == 0){ //如果是第一个零
				$flag = 1; //该位为零
				$str = $ch.$str; //加入汉字数值字符串
				continue;
			}elseif($ch == "零"){ //如果不是第一个零了
				continue;
			}
			$flag = 0; //该位不是零
			switch($j) {
				case 0: $str = $ch; $flag_end = 0; break; //第一位（末尾），没有以“零”结尾
				case 1: $str = $ch."十".$str; break; //第二位
				case 2: $str = $ch."百".$str; break; //第三位
				case 3: $str = $ch."千".$str; break; //第四位
			}
		}
		if($flag_end == 1) //如果以“零”结尾
		{
			mb_internal_encoding("UTF-8");
			$str = mb_substr($str, 0, mb_strlen($str)-1); //把“零”去掉
		}
		return $str;
	}
}
/**
 * 删除数组中多余的空格
 */
if(!function_exists('array_filter_null'))
{
function array_filter_null($param){
	if($param==''){
	    return false;
        }else{
            return true;
        }
	}
}

/**
 * 航空公司简称转换
 */
if(!function_exists('airline_word_to_name'))
{
    function airline_word_to_name($param){

        $data = array('3U'=>'四川航空','BK'=>'奥凯航空','8L'=>'祥鹏航空',
                      'CA'=>'中国航空','CN'=>'大新华航空','CZ'=>'南方航空',
                      'FM'=>'上海航空','HO'=>'吉祥航空','HU'=>'海南航空',
                      'JD'=>'金鹿航空','JR'=>'幸福航空','MF'=>'厦门航空',
                      'MU'=>'东方航空','PN'=>'西部航空','SC'=>'山东航空',
                      'ZH'=>'深圳航空');
        return $data[$param];
    }
}

/**
 * 获取推荐分类的链接地址
 */
if(!function_exists('recommend_link')){
    function recommend_link($name){
        return site_url($name);
    }
}




/**
 * curl/socket 请求函数
 *
 */
if(!function_exists('_dfsockopen')) {

    function _dfsockopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE, $encodetype = 'URLENCODE', $allowcurl = TRUE, $position = 0, $files = array())
    {
        $return = '';
        $matches = parse_url($url);
        $scheme = $matches['scheme'];
        $host = $matches['host'];
        @$path = $matches['path'] ? $matches['path'] . ($matches['query'] ? '?' . $matches['query'] : '') : '/';
        $port = !empty($matches['port']) ? $matches['port'] : ($scheme == 'http' ? '80' : '');
        $boundary = $encodetype == 'URLENCODE' ? '' : random(40);

        if ($post) {
            if (!is_array($post)) {
                parse_str($post, $post);
            }
            _format_postkey($post, $postnew);
            $post = $postnew;
        }

        if ($post) {
            if ($encodetype == 'URLENCODE') {
                $data = http_build_query($post);
            } else {
                $data = '';
                foreach ($post as $k => $v) {
                    $data .= "--$boundary\r\n";
                    $data .= 'Content-Disposition: form-data; name="' . $k . '"' . (isset($files[$k]) ? '; filename="' . basename($files[$k]) . '"; Content-Type: application/octet-stream' : '') . "\r\n\r\n";
                    $data .= $v . "\r\n";
                }
                foreach ($files as $k => $file) {
                    if (!isset($post[$k]) && file_exists($file)) {
                        if ($fp = @fopen($file, 'r')) {
                            $v = fread($fp, filesize($file));
                            fclose($fp);
                            $data .= "--$boundary\r\n";
                            $data .= 'Content-Disposition: form-data; name="' . $k . '"; filename="' . basename($file) . '"; Content-Type: application/octet-stream' . "\r\n\r\n";
                            $data .= $v . "\r\n";
                        }
                    }
                }
                $data .= "--$boundary\r\n";
            }
            $out = "POST $path HTTP/1.0\r\n";
            $header = "Accept: */*\r\n";
            $header .= "Accept-Language: zh-cn\r\n";
            $header .= $encodetype == 'URLENCODE' ? "Content-Type: application/x-www-form-urlencoded\r\n" : "Content-Type: multipart/form-data; boundary=$boundary\r\n";
            $header .= 'Content-Length: ' . strlen($data) . "\r\n";
            @$header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
            $header .= "Host: $host:$port\r\n";
            $header .= "Connection: Close\r\n";
            $header .= "Cache-Control: no-cache\r\n";
            $header .= "Cookie: $cookie\r\n\r\n";
            $out .= $header;
            $out .= $data;
        } else {
            $out = "GET $path HTTP/1.0\r\n";
            $header = "Accept: */*\r\n";
            $header .= "Accept-Language: zh-cn\r\n";
            $header .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
            $header .= "Host: $host:$port\r\n";
            $header .= "Connection: Close\r\n";
            $header .= "Cookie: $cookie\r\n\r\n";
            $out .= $header;
        }

        $fpflag = 0;
        if (!$fp = @fsocketopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout)) {
            $context = array(
                'http' => array(
                    'method' => $post ? 'POST' : 'GET',
                    'header' => $header,
                    'content' => $post,
                    'timeout' => $timeout,
                ),
            );
            $context = stream_context_create($context);
            $fp = @fopen($scheme . '://' . ($ip ? $ip : $host) . ':' . $port . $path, 'b', false, $context);
            $fpflag = 1;
        }

        if (!$fp) {
            return '';
        } else {
            $headers = $oldchar = '';
            stream_set_blocking($fp, $block);
            stream_set_timeout($fp, $timeout);
            @fwrite($fp, $out);
            $status = stream_get_meta_data($fp);
            if (!$status['timed_out']) {
                while (!feof($fp) && !$fpflag) {
                    $header = @fgets($fp);
                    $headers .= $header;
                    if ($header && ($header == "\r\n" || $header == "\n")) {
                        break;
                    }
                }
                $GLOBALS['filesockheader'] = $headers;

                if ($position) {
                    for ($i = 0; $i < $position; $i++) {
                        $char = fgetc($fp);
                        if ($char == "\n" && $oldchar != "\r") {
                            $i++;
                        }
                        $oldchar = $char;
                    }
                }

                if ($limit) {
                    $return = stream_get_contents($fp, $limit);
                } else {
                    $return = stream_get_contents($fp);
                }
            }
            @fclose($fp);
            return $return;
        }
    }

}

/**
 * socket函数
 *
 */

if(!function_exists('fsocketopen')) {
    function fsocketopen($hostname, $port = 80, &$errno, &$errstr, $timeout = 15) {
        $fp = '';
        if(function_exists('fsockopen')) {
            $fp = @fsockopen($hostname, $port, $errno, $errstr, $timeout);
        } elseif(function_exists('pfsockopen')) {
            $fp = @pfsockopen($hostname, $port, $errno, $errstr, $timeout);
        } elseif(function_exists('stream_socket_client')) {
            $fp = @stream_socket_client($hostname.':'.$port, $errno, $errstr, $timeout);
        }
        return $fp;
    }

}

/**
 * curl函数数组处理
 *
 */
if(!function_exists('_format_postkey')) {
    function _format_postkey($post, &$result, $key = '')
    {
        foreach ($post as $k => $v) {
            $_k = $key ? $key . '[' . $k . ']' : $k;
            if (is_array($v)) {
                _format_postkey($v, $result, $_k);
            } else {
                $result[$_k] = $v;
            }
        }
    }
}


/**
 * curl函数数组处理
 *
 */
if(!function_exists('_object_array')) {

    function _object_array($array)
    {
        if (is_object($array)) {
            $array = (array)$array;
        }
        if (is_array($array)) {
            foreach ($array as $key => $value) {
                $array[$key] = _object_array($value);
            }
        }
        return $array;
    }
}